using System;
using System.Collections.Generic;
using EPiServer.Core;

namespace EPiSugar.EntityAdapter
{
    public class PageMapping
    {
        public string PageTypeName { get; private set; }
        public int PageTypeID { get; private set; }
        public Type EntityType { get; private set; }
        public IList<IPropertyBinder> Bindings { get; private set; }
        
        public PageMapping(int pageTypeID, string pageTypeName, Type entityType)
        {
            PageTypeID = pageTypeID;
            PageTypeName = pageTypeName;
            EntityType = entityType;
            Bindings = new List<IPropertyBinder>();
        }

        public virtual object Instantiate()
        {
            return Activator.CreateInstance(EntityType);
        }

        public virtual void Bind(PageData pageData, object instance)
        {
            foreach(IPropertyBinder binder in Bindings)
            {
                binder.Bind(pageData, instance);
            }
        }

        public virtual void AddBinder(IPropertyBinder propertyBinder)
        {
            if (propertyBinder != null)
                Bindings.Add(propertyBinder);
        }

        public override string ToString()
        {
            return PageTypeName + "#" + PageTypeID;
        }
    }
}